import { Injectable } from '@angular/core';
import { RestService } from '@abp/ng.core';
import { ShopWhereDto, ShopWhereSearchInput, ShopWhereNameSearchInput } from './models';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { environment } from '../../environments/environment';

@Injectable({
  providedIn: 'root'
})
export class ShopWhereService {
  apiName = 'Default';
  private apiUrl = environment.apis.default.url;

  constructor(
    private restService: RestService,
    private http: HttpClient
  ) {}

  getList(params: any = {}): Observable<any> {
    console.log('ShopWhereService.getList 被调用，参数:', params);
    
    // 使用正确的API路径
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/shop-where',
      params
    }).pipe(
      tap(response => console.log('获取商品列表响应:', response)),
      catchError(error => {
        console.error('获取商品列表错误:', error);
        return throwError(error);
      })
    );
  }

  getListDirect(params: any = {}): Observable<any> {
    console.log('ShopWhereService.getListDirect 被调用，参数:', params);
    
    // 使用正确的API URL
    const url = `${this.apiUrl}/api/app/shop-where`;
    console.log('直接API URL:', url);
    
    return this.http.get(url, { params }).pipe(
      tap(response => console.log('直接获取商品列表响应:', response)),
      catchError(error => {
        console.error('直接获取商品列表错误:', error);
        return throwError(error);
      })
    );
  }

  searchByName(nameKeyword: string, params: any = {}): Observable<any> {
    console.log('ShopWhereService.searchByName 被调用，关键字:', nameKeyword, '参数:', params);
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/shop-where/search-by-name',
      params: {
        nameKeyword,
        skipCount: params['skipCount'] || 0,
        maxResultCount: params['maxResultCount'] || 10,
        sorting: params['sorting'] || ''
      }
    }).pipe(
      tap(response => console.log('搜索商品响应:', response)),
      catchError(error => {
        console.error('搜索商品错误:', error);
        return throwError(error);
      })
    );
  }

  search(input: ShopWhereSearchInput): Observable<any> {
    console.log('ShopWhereService.search 被调用，输入:', input);
    return this.restService.request<any, any>({
      method: 'GET',
      url: '/api/app/shop-where/search',
      params: input
    }).pipe(
      tap(response => console.log('搜索商品响应:', response)),
      catchError(error => {
        console.error('搜索商品错误:', error);
        return throwError(error);
      })
    );
  }

  getById(ownerCode: string, productCode: string, productSku: string): Observable<ShopWhereDto> {
    console.log('ShopWhereService.getById 被调用，参数:', { ownerCode, productCode, productSku });
    return this.restService.request<any, ShopWhereDto>({
      method: 'GET',
      url: `/api/app/shop-where/get-by-codes?ownerCode=${ownerCode}&productCode=${productCode}&productSku=${productSku}`
    }).pipe(
      tap(response => console.log('获取单个商品响应:', response)),
      catchError(error => {
        console.error('获取单个商品错误:', error);
        return throwError(error);
      })
    );
  }
}